//
// Created by haoba on 2018/3/31.
//

#include "../include/zookeeperServer.jute.h"
#include "../../consensus/include/tk_message.h"
#include <iostream>
#include <chrono>

using namespace std;
using namespace chrono;


int serialize_Tk_command(struct oarchive *out, const char *tag, struct tk_command *v){
    int rc;
    rc = out->start_record(out, tag);
    rc = rc ? rc : out->serialize_Int(out, "scheme", (int*)&v->opcode);
    rc = rc ? rc : out->serialize_Long(out, "scheme", &v->sessionId);
    rc = rc ? rc : out->serialize_Int(out, "scheme", &v->owner);
    rc = rc ? rc : out->serialize_Int(out, "scheme", &v->valSize);
    rc = rc ? rc : out->serialize_String(out, "scheme", &v->val);
    rc = rc ? rc : out->serialize_String(out, "id", &v->val);
    rc = rc ? rc : out->end_record(out, tag);
    return rc;
}
/*int deserialize_Tk_command(struct iarchive *in, const char *tag, struct tk_command *v){
    int rc;
    rc = in->start_record(in, tag);
    rc = rc ? rc : in->deserialize_String(in, "scheme", &v->scheme);
    rc = rc ? rc : in->deserialize_String(in, "id", &v->id);
    rc = rc ? rc : in->end_record(in, tag);
    return rc;
}*/

int serialize_Propose(struct oarchive *out, const char *tag, struct Propose *v){
    int rc;
    rc = out->start_record(out, tag);
    rc = rc ? rc : out->serialize_Int(out, "scheme", (int*)&v->Type);
    rc = rc ? rc : out->serialize_Int(out, "id", &v->CommandId);
    rc = rc ? rc : out->serialize_Long(out, "id", &v->Timestamp);
    rc = rc ? rc : serialize_Tk_command(out, "id", &v->Command);
    rc = rc ? rc : out->end_record(out, tag);
    return rc;
}
/*int deserialize_Propose(struct iarchive *in, const char *tag, struct Propose *v){
    int rc;
    rc = in->start_record(in, tag);
    rc = rc ? rc : in->deserialize_String(in, "scheme", &v->scheme);
    rc = rc ? rc : in->deserialize_String(in, "id", &v->id);
    rc = rc ? rc : in->end_record(in, tag);
    return rc;
}*/

int main(int argc, char **argv){
    struct tk_command command;
    command.valSize = 10;
    command.val = (char*)malloc(10);
    command.sessionId =100;
    command.opcode =GET;
    command.owner =1;

    struct Propose propose;
    propose.Command = command;
    propose.Timestamp = 1000;
    propose.CommandId = 10;
    propose.Type = (TYPE)1;
    struct oarchive * oa;
    oa = create_buffer_oarchive();
    auto start = system_clock::now();
    for(int i = 0;i < 1;i++){
        serialize_Propose(oa,"",&propose);
        int size = get_buffer_len(oa);
        char * result;
        result = get_buffer(oa);
    }
    auto end = system_clock::now();
    auto duration = duration_cast<microseconds>(end - start);
    cout<<"cost time "<<double(duration.count())  <<endl;

    return 0;
}